// pages/recruitIntention/recruitIntention.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '',
    formlist: [
      { isrequire: true, label: '工作性质', key: 'workProperty', valueType: 'text', tips: '请选择', type: 'select',data: ['全职', '兼职'] },
      { isrequire: true, label: '工作地点', key: 'workArea', valueType: 'number', tips: '请选择', type: 'select',mode:'region' },
      { isrequire: true, label: '职位名称', key: 'jobName', valueType: 'text', tips: '请填写', type: 'input'}, 
      { isrequire: true, label: '期望薪资', key: 'expectationSalary', index: [0, 0], valueType: 'text', tips: '请选择，单位K', type: 'select',mode: 'multiSelector' },
      { isrequire: true, label: '工作状态', key: 'workConditing', valueType: 'text', tips: '请选择', type: 'select', data: ['已离职', '在职 ']}
    ],
    formData: {
      workProperty: '',
      workArea: "",
      jobName: '',
      expectationSalary: '',
      workConditing: ''
    },
    canSubmit: false,
  },
  bindPickerChange: function (e) {
    var item = e.currentTarget.dataset.item;
    if (item.data) {
      this.data.formData[item.key] = item.data[e.detail.value]
    } else {
      this.data.formData[item.key] = e.detail.value
    }
    this.setData({
      formData: this.data.formData
    })
    this.changeValue();
  },
  bindMultiPickerChange: function (e) {
    var item = e.currentTarget.dataset.item;
    var index = e.currentTarget.dataset.index;
    this.data.formlist[index].index = e.detail.value;
    this.data.formData[item.key] = item.data[0][e.detail.value[0]] + 'K-' + item.data[1][e.detail.value[1]]+'K';
    this.setData({
      formData: this.data.formData
    })
    this.changeValue();
  },
  bindColumnchange: function (e) {
    var item = e.currentTarget.dataset.item;
    var index = e.currentTarget.dataset.index;
    if (e.detail.column == 0) {
      var arr = [];
      arr = arr.concat(item.data[0].slice(e.detail.value, item.data[0].length));
      this.setData({
        ['formlist[' + index + '].data[1]']: arr
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo')),
      // formData: that.data.formData
    })
    this.getUserIdentity();
  },
  getUserIdentity: function () {
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'recruitPersonalInfo/getOne', //仅为示例，并非真实的接口地址
      data: { userId: this.data.userInfo.id },
      success: function (res) {
        if (res.data.success) {
          if (res.data.data) {
            for (var j in that.data.formData) {
              if (res.data.data[j] !== '' && res.data.data[j] != undefined && res.data.data[j] != null) {
                if (j == 'workArea') {
                  that.data.formData[j] = res.data.data[j].split(',');
                } else {
                  that.data.formData[j] = res.data.data[j];
                }
              }
            }
            that.data.formData.id = res.data.data.id;
            that.setData({
              formData: that.data.formData
            })
            that.setSelectValue('workProperty',0);
            that.setSelectValue('workConditing', 4);
            that.getMonthIncome();
            that.changeValue();
          }
        }
      }
    })
  },
  setSelectValue: function (key, index) {
    if (this.data.formData[key] !== '') {
      var that = this;
      this.data.formlist[index].data.forEach(function (ele, j) {
        if (ele == that.data.formData[key]) {
          that.setData({
            ['formlist[' + index + '].index']: j,
            ['showData.' + key]: ele
          })
        }
      })
    }
  },
  getMonthIncome: function () {
    var arr = [];
    for (var i = 1; i <= 50; i++) {
      arr.push(i);
    }
    arr.push('50k以上');
    if (this.data.formData.expectationSalary != '') {
      if (this.data.formData.expectationSalary.indexOf('-') >= 0) {
        var monthIncome = this.data.formData.expectationSalary.split('-');
        var heightiIndex = [0, 0];
        arr.forEach(function (ele, index) {
          if (ele == monthIncome[0].substring(0, monthIncome[0].length-1)) {
            heightiIndex[0] = index;
          }
          if (ele == monthIncome[1].substring(0, monthIncome[1].length - 1)) {
            heightiIndex[1] = index;
          }
        })
        this.setData({
          ['formlist[3].index']: heightiIndex,
        })
      }
    }
    this.setData({
      ['formlist[3].data']: [arr, arr]
    })
  },
  changeTextarea: function (e) {//输入框值改变事件
    var key = e.target.dataset.value;
    this.data.formData[key] = e.detail.value;
    this.setData({
      formData: this.data.formData
    })
    this.changeValue();
  },
  saveBasicInfo: function (e) {
    if (this.data.canSubmit) {
        var param = {};
        for (var j in this.data.formData) {
          param[j] = this.data.formData[j];
        }
        if (param.workArea != '') {
          if (param.workArea.indexOf(',') < 0) {
            param.workArea = param.workArea.join(',');
          }
        }
        var that = this;
        wx.request({
          url: app.globalData.httpApiCar + 'recruitPersonalInfo/update', //仅为示例，并非真实的接口地址
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          data: param,
          success: function (res) {
            if (res.data.success) {
              // wx.redirectTo({
              //   url: '/pages/recruitMine/recruitMine',
              // })
              wx.navigateBack({
                delta: 1
              })
            }
            else {
              wx.showToast({
                title: res.data.error,
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
    }
  },
  changeValue() {
    var flag = true;
    var item = this.data.formData;
    if ((item.workProperty == '') || (item.workArea == '') || (item.jobName == '') || (item.expectationSalary == '') || (item.workConditing == '')) {
      flag = false;
    }
    this.setData({
      canSubmit: flag
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})